package com.evon.yardmanagement.ui.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.evon.yardmanagement.model.Transporter;
import com.evon.yardmanagement.model.Yard;
import com.evon.yardmanagement.service.TransporterService;
import com.evon.yardmanagement.service.YardManagementService;
import com.evon.yardmanagement.transformer.BaseTransformer;
//import com.evon.yardmanagement.ui.Active;
import com.evon.yardmanagement.ui.dto.TransporterDTO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

public class TransporterAction extends ActionSupport implements ApplicationAware{

	private TransporterDTO dto;
	BaseTransformer<Transporter, TransporterDTO> baseTransformer = new BaseTransformer<Transporter, TransporterDTO>(
			Transporter.class, TransporterDTO.class);

	public TransporterDTO getDto() {
		return dto;
	}
	@SkipValidation
	public void setDto(TransporterDTO dto) {
		this.dto = dto;
	}

	public List<Yard> listYard = new ArrayList<Yard>();
	TransporterService service = new TransporterService();
	private int selectedYardId;

	private int oemType;
	
	public int getOemType() {
		return oemType;
	}
	@SkipValidation
	public void setOemType(int oemType) {
		this.oemType = oemType;
	}
	
	public int getSelectedYardId() {
		return selectedYardId;
	}
	@SkipValidation
	public void setSelectedYardId(int selectedYardId) {
		this.selectedYardId = selectedYardId;
	}

	public TransporterAction() {
		dto = new TransporterDTO();
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * This method will initialize transporter dto.
	 * 
	 * @return
	 */
	@SkipValidation
	public String init() {
		dto = new TransporterDTO();
		listYard = new YardManagementService().getAllYards();

		return SUCCESS;
	}

	@SkipValidation
	public String show() {
		if (dto.getId() != 0) {
			System.err.println("FOUND IDDDDDDDD" + dto.getId());
			ActionContext.getContext().getValueStack();
			dto = 
					baseTransformer
					.transformToDTO(service.getTransporterDetails(dto.getId()));
			

		}
		return SUCCESS;
	}

	@Validations(
			requiredStrings = { 
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.name", message = "Please enter Transporter Name."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.city", message = "Please enter City."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.phone", message = "Please enter Phone."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.description", message = "Please enter Description."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.address", message = "Please enter Address."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.contactPerson", message = "Please enter Contact Person.")
			},
			regexFields = {					
					@RegexFieldValidator(fieldName ="dto.name", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for name."),
					@RegexFieldValidator(fieldName ="dto.description", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for discription."),
					@RegexFieldValidator(fieldName ="dto.contactPerson", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for Contact Person."),
					@RegexFieldValidator(fieldName ="dto.city", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for City name."),
					@RegexFieldValidator(fieldName ="memberDTO.address", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for Address."),
					@RegexFieldValidator(fieldName="dto.phone", expression="^[0-9]+",message="Phone can only be numeric", type=ValidatorType.FIELD)
					//@RegexFieldValidator(fieldName ="memberDTO.middleName", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for Middle name."),
					//@RegexFieldValidator(fieldName ="sectionDTO.capacity", expression = "^[0-9]+", message = "Slot can only be numeric")
					}
	)
	public String execute() throws Exception {
		/*if(!isValidInput(dto)){
			return INPUT;
		}*/
		
		Transporter transientTransporter = baseTransformer.transformToModel(dto);
		 transientTransporter.setYardId(selectedYardId);
		 
		service.addTransporter(transientTransporter);
		addActionMessage("Transporter Added successfully");
		return SUCCESS;
	}

	private boolean isValidInput(TransporterDTO dto2) {
		//Validate all input parameters.
		if(dto.getName() == null || dto.getName().trim().equals("")){
			addActionError("Please enter Transporter Name");
			return false;
		}
		if(dto.getCity() == null || dto.getCity().trim().equals("")){
			addActionError("Please enter City");
			return false;
		}
		
		if(dto.getPhone() == null || dto.getPhone().trim().equals("")){
			addActionError("Please enter Phone");
			return false;
		}
		if(dto.getDescription() == null || dto.getDescription().trim().equals("")){
			addActionError("Please enter Description");
			return false;
		}
		if(dto.getAddress() == null || dto.getAddress().trim().equals("")){
			addActionError("Please enter Address");
			return false;
		}
		if(dto.getContactPerson() == null || dto.getContactPerson().trim().equals("")){
			addActionError("Please enter Contact Person");
			return false;
		}
		return true;
	}

	@SkipValidation
	public void setApplication(Map<String, Object> application) {
		listYard = (List<Yard>) application.get("YARDS");
		
	}

}
